package com.example.neusoft.zyfypt_wk.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.neusoft.zyfypt_wk.R;
import com.example.neusoft.zyfypt_wk.activity.CollectActivity;
import com.example.neusoft.zyfypt_wk.activity.FocusActivity;
import com.example.neusoft.zyfypt_wk.activity.LoginActivity;
import com.example.neusoft.zyfypt_wk.adapter.ArticleAdapter;
import com.example.neusoft.zyfypt_wk.bean.ArticleBean;
import com.example.neusoft.zyfypt_wk.bean.LoginBean;
import com.example.neusoft.zyfypt_wk.iface.ArticleListener;
import com.example.neusoft.zyfypt_wk.model.ArticleModel;
import com.example.neusoft.zyfypt_wk.service.UserService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by Neusoft on 2017/4/9.
 */
/*我的*/
public class Fragment5 extends Fragment {
    private Button btncollect,btnfocus,btnlogout;
    private TextView tvinfo;
    private View view=null;
    private Context context;



    private SharedPreferences sp;
    private String sessionID="";
    private String name="";

    private View.OnClickListener listener=new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId())
            {
                case R.id.button://收藏
                    Intent intent=new Intent(context, CollectActivity.class);
                    startActivity(intent);
                    break;
                case R.id.button2://关注
                    Intent intent2=new Intent(context, FocusActivity.class);
                    startActivity(intent2);
                    break;
                case R.id.button3://注销
                    logout();
                    break;
            }
        }
    };



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        context=getActivity();
       //动态加载Fragment5的布局文件
        view=inflater.inflate(R.layout.fragment5,container,false);
        init();
        sp=context.getSharedPreferences("login",MODE_PRIVATE);
        readSP();
        //返回动态生成的view
        return view;
    }

    private void readSP() {
        sessionID=sp.getString("sessionID",null);
        name=sp.getString("name",null);
        tvinfo.setText(name+" 欢迎你!");

    }
    private void init() {
        btncollect=(Button)view.findViewById(R.id.button);
        btnfocus=(Button)view.findViewById(R.id.button2);
        btnlogout=(Button)view.findViewById(R.id.button3);
        btncollect.setOnClickListener(listener);
        btnfocus.setOnClickListener(listener);
        btnlogout.setOnClickListener(listener);

        tvinfo=(TextView)view.findViewById(R.id.textView9);

    }

    private void logout() {
        Retrofit retrofit;
        String BASEURL
                ="http://amicool.neusoft.edu.cn/";
        //使用Retrofit----1
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        UserService userService
                =retrofit.create(UserService.class);
        Call<String> call
                =userService.logout(sessionID);
        //使用Retrofit----3
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                Intent intent=new Intent(context, LoginActivity.class);
                startActivity(intent);
                getActivity().finish();
            }
            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });

    }

}
